<?php
/**
 * Created by PhpStorm.
 * User: Yui
 * Date: 2017/5/19 0019
 * Time: 11:25
 */

namespace common\services;

use common\library\comFun;
use Yii;
use  common\models\Work;
use yii\data\Pagination;

class WorkSve extends BaseSve
{
    public function add(array $data, string $scenario = ''):array
    {
        return parent::add($data, $scenario);
    }

    public function getWork($where = [], string $orderBy = ''):array
    {
        $Work = Work::find()->where($where)->orderBy($orderBy)->asArray()->one();
        if ($Work == false) {
            $Work = [];
        }
        return $Work;
    }

    public function remove($where):array
    {
        $Work = Work::find()->where($where)->one();
        if (empty($Work)) {
            return self::result('删除成功', 0);
        }

        $rs = $Work->delete();

        if ($rs === false) {
            throw new \Exception('删除失败', -1);
        }
        return self::result('删除成功', 0);
    }

    public function getShowList($where,$orderBy)
    {
        //处理模型名
        $Work = Work::find()->groupBy('member_id');
        if (!empty($where)) {
            $Work->where($where);
        }
        //要返回的数据
        $returnData = [];
        //是
        //查询总数
        $count = $Work->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $request = Yii::$app->request;
        $pageSize = $request->get('page_size', 20);
        if (!comFun::isPositiveInt($pageSize)) {
            //如果分页大小不符合规范，默认20
            $pageSize = 20;
        }
        $pagination->pageSize = $pageSize;

        $page = $request->get('page', 1);

        if (!comFun::isPositiveInt($page)) {
            //如果页数不符合规范，默认1
            $page = 1;
        }

        $pagination->page = --$page;
        $list = $Work->orderBy($orderBy)
            ->offset($pagination->offset)
            ->limit($pagination->limit)->all();
        $returnData['list'] = $list;
        $returnData['pageArr'] = [
            'pageSize' => $pagination->pageSize,//分页大小
            'total' => $pagination->totalCount,//总条数
            'pageTotal' => ceil($pagination->totalCount / $pagination->pageSize),//分页数
            'currentPage' => ++$page//当前页
        ];

        return self::result('', 0, $returnData);

    }

    public function getShowTvList($where,$orderBy)
    {
        //处理模型名
        $now = time();
        $today = strtotime(date('Y-m-d'));
        $Work = Work::find()->alias('w')->select('w.*')
            ->leftJoin('member m', 'w.member_id = m.member_id')
            ->where(" ((w.start_time > {$today} and w.end_time > {$now}) or w.end_time > {$now}) and (m.status=1 and m.is_del=0 and w.work_class_id in (1,2))")
            ->groupBy('w.member_id')
            ->orderBy('w.work_id');

        //要返回的数据
        $returnData = [];
        //是
        //查询总数
        $count = $Work->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $request = Yii::$app->request;
        $pageSize = $request->get('page_size', 20);
        if (!comFun::isPositiveInt($pageSize)) {
            //如果分页大小不符合规范，默认20
            $pageSize = 20;
        }
        $pagination->pageSize = $pageSize;

        $page = $request->get('page', 1);

        if (!comFun::isPositiveInt($page)) {
            //如果页数不符合规范，默认1
            $page = 1;
        }

        $pagination->page = --$page;
        $list = $Work->orderBy($orderBy)
            ->offset($pagination->offset)
            ->limit($pagination->limit)->all();
        $returnData['list'] = $list;
        $returnData['pageArr'] = [
            'pageSize' => $pagination->pageSize,//分页大小
            'total' => $pagination->totalCount,//总条数
            'pageTotal' => ceil($pagination->totalCount / $pagination->pageSize),//分页数
            'currentPage' => ++$page//当前页
        ];

        return self::result('', 0, $returnData);

    }

}